package to52.android.screens;

import to52.android.database.RetreiveData;
import to52.android.preferences.Preferences;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


public class MainScreen extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		RetreiveData rd = new RetreiveData(this);
		if (rd.getPatients().size() < 1) {
			rd.insertDatas();
		}
		rd.close();
		
        // fix preferences
        
        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        String login = prefs.getString("login", "");
        String password = prefs.getString("password", "");
        String ipServer = prefs.getString("ipAddress", "");
        
        if ( login.length() == 0 || password.length() == 0 || ipServer.length() == 0) {
        	
        	Log.d(this.getClass().getName(), "we need to fix login & pass");
        	
        	LayoutInflater factory = LayoutInflater.from(this);
            final View textEntryView = factory.inflate(R.layout.alert_identify, null);
            new AlertDialog.Builder(this)
                .setTitle("Entrez vos identifiants de connexion")
                .setView(textEntryView)
                .setPositiveButton("Valider", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                    	final TextView tv_login = (TextView) textEntryView.findViewById(R.id.username_edit);
                    	final TextView tv_pass  = (TextView) textEntryView.findViewById(R.id.password_edit);
                    	final TextView tv_ipAddress = (TextView) textEntryView.findViewById(R.id.ipAddress_edit);
                    	
                    	SharedPreferences.Editor prefEditor = prefs.edit();
                    	prefEditor.putString("login", tv_login.getText().toString());
                    	prefEditor.putString("password", tv_pass.getText().toString());
                    	prefEditor.putString("ipAddress", tv_ipAddress.getText().toString());
                        prefEditor.commit();
                    	
                    }
                })
                .setNegativeButton("Annuler", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                    	Toast.makeText(getApplicationContext(), "Vous devez configurer vos identifiants pour utiliser l'application", Toast.LENGTH_LONG).show();
                       
                    }
                })
                .create()
                .show();
        }
		
		
		final Button patientList = (Button) findViewById(R.id.btn_patient_list);
		patientList.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Log.i(this.getClass().getName(), "Launching patient list");
				Intent i = new Intent();
				i.setClass(getApplicationContext(), PatientList.class);
				startActivity(i);
			}
		});

	}

	
	/**
	 * Création du menu qui permet d'ajouter un contact
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.mainmenu, menu);
		return true;
	}

	/**
	 * Sélection d'un item du menu
	 */
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.editpref:
			Intent myIntent = new Intent(getApplicationContext(),
					Preferences.class);
			startActivityForResult(myIntent, 0);
			return true;
		case R.id.sync:
			to52.android.services.PullService.setActivity(this);
			if ( to52.android.services.PullService.getInstance() == null) {
	        	startService(new Intent(MainScreen.this, to52.android.services.PullService.class));
	        }
			return true;
		case R.id.resetDb:
			RetreiveData rd = new RetreiveData(this);
			rd.clearDatabase();
			RetreiveData.patients = null;
			rd.insertDatas();
			return true;
		}

		return false;
	}
	
	public static void killPullService() {
		if ( to52.android.services.PullService.getInstance() != null) {
			to52.android.services.PullService.killInstance();
        }
	}
	
}

